// pages/dynamicLink/dynamicLink.js
var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据 
   */
  data: {
    readPercent:0
  },
  joinUs:function(e){
    var that = this;
    wx.navigateTo({
      url: '/pages/joinUs/joinUs?source=aiticle&source_txt=来自文章'+that.data.news.article_title+'联系我们',
    })
  },  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var article_id = options.article_id || 0;
    app.httpRequest('article.article/detail', {article_id: article_id}, 'get', false, function(res){
      var article = res.data.detail.article_content
      WxParse.wxParse('article', 'html', article, that, 5);
      res.data.detail.article_content = article;
      wx.setNavigationBarTitle({
        title: res.data.detail.article_title
      });
      that.setData({
        news: res.data.detail,
        pageShow: true
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var startTime = new Date().getTime();
    that.setData({
      startTime: startTime,
      windowHeight: wx.getStorageSync('systemInfo').windowHeight
    })
  },

  // 获得停留时间，毫秒级
  getStopTime: function () {
    var that = this;
    var endTime = new Date().getTime();
    var startTime = that.data.startTime;
    var stopTime = endTime - startTime;
    that.setData({
      stopTime: stopTime
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    that.getStopTime();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    that.getStopTime();
    var stoptime =  that.data.stopTime;
    var percent = that.data.readPercent;
    app.userBehavior(123,'浏览文章《'+that.data.news.article_title+"》",stoptime,percent)
  },
  
  // 滚动计算阅读
  scroll: function (e) {
    var that = this;
    var scrollHeight = e.detail.scrollHeight;
    var scrollTop = e.detail.scrollTop;
    var windowHeight = that.data.windowHeight;
    var height = scrollHeight - windowHeight - 20;
    var scrollPercent = (scrollTop / height) * 100;
    scrollPercent = Math.ceil(scrollPercent);
    var readPercent = that.data.readPercent;
    if (scrollPercent > 100) {
      scrollPercent = 100;
      readPercent = 100;
    } else {
      scrollPercent = scrollPercent;
      if (scrollPercent > readPercent) {
        readPercent = scrollPercent;
      } else {
        readPercent = readPercent;
      }
    }
    that.setData({
      readPercent: readPercent
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
    app.userBehavior(124,'分享文章《'+that.data.news.article_title+'》')
    return {
      title: that.data.news.article_title,
      path: "/pages/dynamicLink/dynamicLink?share_card_id=" + wx.getStorageSync('share_card_id') + "&share_user_id=" + wx.getStorageSync('user_id') + '&article_id=' + that.data.news.article_id,
      success: function (a) {
      },
      fail: function (a) { }
    };
  }
})